/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: task_manager_proxy.hpp
 */

#ifndef _TASK_MANAGER_PROXY_HPP_
#define _TASK_MANAGER_PROXY_HPP_

#include <iostream>
#include <string>

#include "task_descriptor.hpp"
#include "proxy_exception.hpp"
#include "task_manager.hpp"

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
/*! \struct ManProxyConfig
 * \brief Configuration for TaskManagerProxy.
 *
 * Configuration structure for TaskManagerProxy.
 */
typedef struct {
	/*!
	 * Defines the Ip v4 address to connect to the Manager of another peer to
	 * send a TaskDescriptor.
	 */
	std::string ip_to_man; /* IP used to send to manager of another peer */
	/*!
	 * Defines the TCP Port to connect to the Manager of another peer to
	 * send a TaskDescriptor.
	 */
	std::string port_to_man; /* PORT used to send to manager of another peer */
	/*!
	 * Ip v4 address used to bind in order to receive a TaskDescriptor from
	 * another Manager of another peer.
	 */
	std::string ip_bind_man; /* IP to bind to in order to listen for tasks */
	/*!
	 * TCP port used to bind in order to receive a TaskDescriptor from
	 * another Manager of another peer.
	 */
	std::string port_bind_man; /* PORT to bind to in order to listen for tasks */
} ManProxyConfig;
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*! \class TaskManagerProxy
 * \brief Proxy to let a Manager communicate with another Manager.
 *
 * This class provides network interface to send and receive TaskDescriptor. It is
 * important to notice that constructor is private in order to let a factory
 * perform instantiation of an object of this class.
 */
class TaskManagerProxy : public TaskManager {
	/*!
	 * To let factory build properly this object, we provide access to every
	 * part of it.
	 */
	friend class TaskProxyFactory;
public:
	/*!
	 * Destructor
	 */
	~TaskManagerProxy();
	/*!
	 * Sends a TaskDescriptor to another Manager on another peer.
	 * \param task The TaskDescriptor to send.
	 * \exception ProxyException If it is not possible to send TaskDescriptor,
	 * this exception is thrown.
	 */
	void send_task(const TaskDescriptor& task) throw(ProxyException);
	/*!
	 * Receives a TaskDescriptor from another Manager on another peer. This call
	 * causes the caller to block until something arrives.
	 * \return Returns the TaskDescriptor received.
	 * \exception ProxyException This exception is thrown when some errors
	 * occurred on the channel or the stream has been closed by the other
	 * Manager after connection establishment.
	 */
	TaskDescriptor recv_task() throw(ProxyException);
private:
	/*!
	 * Privately constructs the object. Default configuration with local
	 * loopback address and invalid port.
	 */
	TaskManagerProxy();
	/*!
	 * Privately constructs the object. Assigning configuration.
	 */
	explicit TaskManagerProxy(ManProxyConfig conf);
	/*!
	 * Parameters for proxy.
	 */
	ManProxyConfig _config;
}; /* TaskManagerProxy */
} /* middleware */

#endif
